<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/xhtml;charset=UTF-8"/>
<title>AngelScript: Multithreading</title>

<link href="tabs.css" rel="stylesheet" type="text/css"/>
<link href="doxygen.css" rel="stylesheet" type="text/css" />
<link href="navtree.css" rel="stylesheet" type="text/css"/>
<script type="text/javascript" src="jquery.js"></script>
<script type="text/javascript" src="resize.js"></script>
<script type="text/javascript" src="navtree.js"></script>
<script type="text/javascript">
  $(document).ready(initResizable);
</script>


</head>
<body>
<div id="top"><!-- do not remove this div! -->


<div id="titlearea">
<table cellspacing="0" cellpadding="0">
 <tbody>
 <tr style="height: 56px;">
  
  
  <td style="padding-left: 0.5em;">
   <div id="projectname">AngelScript
   
   </div>
   
  </td>
  
  
  
   
  
 </tr>
 </tbody>
</table>
</div>

<!-- Generated by Doxygen 1.7.5.1 -->
</div>
<div id="side-nav" class="ui-resizable side-nav-resizable">
  <div id="nav-tree">
    <div id="nav-tree-contents">
    </div>
  </div>
  <div id="splitbar" style="-moz-user-select:none;" 
       class="ui-resizable-handle">
  </div>
</div>
<script type="text/javascript">
  initNavTree('doc_adv_multithread.html','');
</script>
<div id="doc-content">
<div class="header">
  <div class="headertitle">
<div class="title">Multithreading </div>  </div>
</div>
<div class="contents">
<div class="textblock"><p>AngelScript supports multithreading, though not yet on all platforms. You can determine if multithreading is supported on your platform by calling the <a class="el" href="angelscript_8h.html#aba86cba765a7148e2a306b4305ba48f9">asGetLibraryOptions</a> function and checking the returned string for <code>"AS_NO_THREADS"</code>. If the identifier is in the returned string then multithreading is not supported.</p>
<p>Even if you don't want or can't use multithreading, you can still write applications that execute <a class="el" href="doc_adv_concurrent.html">multiple scripts simultaneously</a>.</p>
<h2><a class="anchor" id="doc_adv_multithread_1"></a>
Things to think about with a multithreaded environment</h2>
<ul>
<li>Always call <a class="el" href="angelscript_8h.html#a51079811680d5217046aad2a2b695dc7">asThreadCleanup</a> before terminating a thread that accesses the script engine. If this is not done, the memory allocated specifically for that thread will be lost until the script engine is freed.</li>
</ul>
<ul>
<li>Multiple threads may execute scripts in separate contexts. The contexts may execute scripts from the same module, but if the module has global variables you need to make sure the scripts perform proper access control so that they do not get corrupted, if multiple threads try to update them simultaneously.</li>
</ul>
<ul>
<li>The engine will only allow one thread to build scripts at any one time, since this is something that changes the internal state of the engine and cannot safely be done in multiple threads simultaneously.</li>
</ul>
<ul>
<li>Do not execute the <a class="el" href="doc_gc.html">garbage collector</a> while other script threads are executed, as the garbage collector may be traversing the reference graph while the objects are modified by the other threads.</li>
</ul>
<ul>
<li>Resources that are shared by script modules such as registered properties and objects must be protected by the application for simultaneous access, as the script engine doesn't do this automatically.</li>
</ul>
<h2><a class="anchor" id="doc_adv_fibers"></a>
Fibers</h2>
<p>AngelScript can be used with fibers as well. However, as fibers are not real threads you need to be careful if multiple fibers need to execute scripts. AngelScript keeps track of active contexts by pushing and popping the context pointers on a stack. With fibers you can easily corrupt this stack if you do not pay attention to where and when script executions are performed.</p>
<p>Try to avoid switching fibers when a script is executing, instead you should <a class="el" href="classas_i_script_context.html#ad4ac8be3586c46069b5870e40c86544a">suspend</a> the current script execution and only switch to the other fiber when the context's <a class="el" href="classas_i_script_context.html#a8e52894432737acac2e1a422e496bf84">Execute()</a> method returns. If you really must switch fibers while executing a script, then make sure the second fiber initiates and concludes its own script execution before switching back to the original fiber. </p>
</div></div>
</div>
  <div id="nav-path" class="navpath">
    <ul>

    <li class="footer">Generated on Sun Jan 29 2012 15:41:03 for AngelScript by
    <a href="http://www.doxygen.org/index.html">
    <img class="footer" src="doxygen.png" alt="doxygen"/></a> 1.7.5.1 </li>
   </ul>
 </div>


</body>
</html>
